﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Entities.Addresses
{
    [Serializable]
    public class AddressExtractorOutput:ResultBase
    {
        #region props
        private int _SrcRecordCount;
        /// <summary></summary>
        public int SrcRecordCount
        {
            get
            {
                return this._SrcRecordCount;
            }
            set
            {
                this._SrcRecordCount = value;
            }
        }

        private int _AddrRecordCount;
        /// <summary></summary>
        public int AddrRecordCount
        {
            get
            {
                return this._AddrRecordCount;
            }
            set
            {
                this._AddrRecordCount = value;
            }
        }

        private int _USAddrRecordCount;
        /// <summary></summary>
        public int USAddrRecordCount
        {
            get
            {
                return this._USAddrRecordCount;
            }
            set
            {
                this._USAddrRecordCount = value;
            }
        }

        private int _LocatableAddrCount;
        /// <summary>
        /// either one of the following is satisfied
        /// organization
        /// city+region
        /// postal code
        /// </summary>
        public int LocatableAddrCountPosition
        {
            get
            {
                return this._LocatableAddrCount;
            }
            set
            {
                this._LocatableAddrCount = value;
            }
        }
        #endregion

        public AddressExtractorOutput():base(Guid.Empty, Guid.Empty)
        {
            this._SrcRecordCount = 0;
            this._AddrRecordCount = 0;
            this._USAddrRecordCount = 0;
            this._LocatableAddrCount = 0;
        }

        public AddressExtractorOutput(Guid componentID, Guid jobTicketID,
            int srcRecCount, int addrRecCount, 
            int usAddrCount, int locatableAddrCount) 
            : base(componentID, jobTicketID)
        {
            this._SrcRecordCount = srcRecCount;
            this._AddrRecordCount = addrRecCount;
            this._USAddrRecordCount = usAddrCount;
            this._LocatableAddrCount = locatableAddrCount;
        }

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                AddressExtractorOutput output = new AddressExtractorOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.SrcRecordCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "SrcRecCount", "0"));
                output.AddrRecordCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "AddrRecCount", "0"));
                output.USAddrRecordCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "USAddrCount", "0"));
                output.LocatableAddrCountPosition =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "LocatableAddrCount", "0"));
                
                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcRecCount", this.SrcRecordCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "AddrRecCount", this.AddrRecordCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "USAddrCount", this.USAddrRecordCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LocatableAddrCount", this.LocatableAddrCountPosition.ToString());

            return xNode;
        }
    }
}
